#include "Encoder.h"

/*重定义*/
int Encoder_GetPosition(void){       //返回编码器寄存器里的值
    /*获取编码器数值*/
    int out;
    out =  (int)DL_TimerG_getTimerCount(Encoder_INST);     //可得到定时器的计数值
    // DL_TimerG_startCounter(Encoder_INST);
    return out;
}
void Encoder_ResetPosition(void){       //归零计数器
    /*编码器寄存器清空*/
    // 停止计数器（可选，但建议操作，避免冲突）
    DL_Timer_stopCounter(Encoder_INST);
    
    // 配置启动后从 0 开始计数
    DL_Timer_setCounterValueAfterEnable(Encoder_INST, DL_TIMER_COUNT_AFTER_EN_ZERO);
    
    // 启动计数器，编码器计数从 0 开始
    DL_Timer_startCounter(Encoder_INST);
}
/*APP*/
#define BSP_Encode_step (0.25)       //编码器值每次该变的大小
static Encoder_data BSP_Encode_Data;       //确保只在该文件中被调用
static uint8_t BSP_Encode_Index = 0;        //指向运行的编码器值
static float BSP_Encode_buffer = 0;         //编码器值缓存

int Encoder_Getdata(){     //返回处理后的数据
    int out;
    out =  (int)DL_TimerG_getTimerCount(Encoder_INST);     //可得到定时器的计数值
    if(out >= 5000){
        out -= 65535;
    }
    return out;
}

void Encoder_Init(){    //初始化编码器数据值
    BSP_Encode_Data.Encode1 = 0;
    BSP_Encode_Data.Encode2 = 0;
    BSP_Encode_Data.Encode3 = 0;
    BSP_Encode_Data.Encode4 = 0;
    BSP_Encode_Index = 1;       //默认指向1
}

Encoder_data* Encoder_Get_ALL(void){      //返回值所有的值
    //更新值
    if(BSP_Encode_Index == 1){
        BSP_Encode_Data.Encode1 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }
    else if(BSP_Encode_Index == 2){
        BSP_Encode_Data.Encode2 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }
    else if(BSP_Encode_Index == 3){
        BSP_Encode_Data.Encode3 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }
    else if(BSP_Encode_Index == 4){
        BSP_Encode_Data.Encode4 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }
    //返回地址
    return &BSP_Encode_Data;
}
float Encoder_Get_Sin(uint8_t ID){      //返回值单一值
    //更新值
    if(BSP_Encode_Index == 1){
        BSP_Encode_Data.Encode1 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }
    else if(BSP_Encode_Index == 2){
        BSP_Encode_Data.Encode2 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }
    else if(BSP_Encode_Index == 3){
        BSP_Encode_Data.Encode3 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }
    else if(BSP_Encode_Index == 4){
        BSP_Encode_Data.Encode4 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }
    //返回地址
    if(ID == 1){
        return BSP_Encode_Data.Encode1;
    }
    else if(ID == 2){
        return BSP_Encode_Data.Encode2;
    }
    else if(ID == 3){
        return BSP_Encode_Data.Encode3;
    }
    else if(ID == 4){
        return BSP_Encode_Data.Encode4;
    }
}
void Encoder_switch(uint8_t ID){        //切换编码器目标,ID = 0,1-2切换，3-4切换
    //切换之前更新一下值
    if(BSP_Encode_Index == 1){
        BSP_Encode_Data.Encode1 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }
    else if(BSP_Encode_Index == 2){
        BSP_Encode_Data.Encode2 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }
    else if(BSP_Encode_Index == 3){
        BSP_Encode_Data.Encode3 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }
    else if(BSP_Encode_Index == 4){
        BSP_Encode_Data.Encode4 = (BSP_Encode_buffer + Encoder_Getdata()*BSP_Encode_step);
    }    
    //切换
    BSP_Encode_Index = ID;
    //更新缓存
    if(BSP_Encode_Index == 1){
        BSP_Encode_buffer = BSP_Encode_Data.Encode1;
    }
    else if(BSP_Encode_Index == 2){
        BSP_Encode_buffer = BSP_Encode_Data.Encode2;
    }
    else if(BSP_Encode_Index == 3){
        BSP_Encode_buffer = BSP_Encode_Data.Encode3;
    }
    else if(BSP_Encode_Index == 4){
        BSP_Encode_buffer = BSP_Encode_Data.Encode4;
    }    
    else{
        if(BSP_Encode_Index == 1){
            BSP_Encode_Index = 2;
        }
        else if(BSP_Encode_Index == 2){
            BSP_Encode_Index = 1;
        }
        else if(BSP_Encode_Index == 3){
            BSP_Encode_Index = 4;
        }
        else if(BSP_Encode_Index == 4){
            BSP_Encode_Index = 3;
        }    
    }
    //归零
    Encoder_ResetPosition();    
}

// //功能测试功能
// void BSP_Encode_Test(){
//     printf("编码器测试代码开启");
//     Encoder_Init()
    
// }